﻿using System;
using Microsoft.VisualStudio.SharePoint;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio;
using System.Collections.Generic;
using System.ComponentModel;
using EnvDTE;

namespace CodeCounsel.SharePoint.DevTools.ItemTypes
{
    [Export(typeof(ISharePointProjectItemTypeProvider))]
    [SharePointProjectItemType("CodeCounsel.SharePoint.DevTools.SPMetalDefinition")]
    public class SPMetalDefinition
        : ISharePointProjectItemTypeProvider
    {
        IServiceProvider _serviceProvider;

        public void InitializeType(ISharePointProjectItemTypeDefinition typeDefinition)
        {
            _serviceProvider = typeDefinition.ProjectService.ServiceProvider;
            typeDefinition.Name = "SPMetalDefinition";
            typeDefinition.SupportedDeploymentScopes = SupportedDeploymentScopes.Package;
            typeDefinition.SupportedTrustLevels = SupportedTrustLevels.All;
            typeDefinition.ProjectItemPropertiesRequested += TypeDefinition_ProjectItemPropertiesRequested;
        }

        internal void ForceRegenerate(ISharePointProjectItem owner)
        {
            //DTE dte = ProjectUtilities.GetDTE();
            //dte.ExecuteCommand("Project.RunCustomTool");
        }

        void TypeDefinition_ProjectItemPropertiesRequested(object sender, SharePointProjectItemPropertiesRequestedEventArgs e)
        {
            SPMetalProperties properties = e.ProjectItem.GetAnnotation<SPMetalProperties>();
            if (properties == null)
            {
                properties = new SPMetalProperties(this, e.ProjectItem, e.ProjectItem.ExtensionData);
                e.ProjectItem.Annotations.Add<SPMetalProperties>(properties);
            }
            e.PropertySources.Add(properties);
        }
    }
}